package com.example.demo.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;

import java.io.IOException;
import java.util.List;

/**
 * 软件著作权:王艳明
 * 系统名称:SpringBoot后台管理系统
 *
 * @author WangYanming
 * @ClassName 类:JacksonUtil.java
 * @Description 功能描述:JacksonUtil
 * @Version 1.0
 * @date 2024-04-13 下午 02:01
 */
public class JacksonUtil {

    private static final Logger log = LoggerFactory.getLogger(JacksonUtil.class);

    public static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        // 去除json中没有的字段
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 美化输出功能
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    }

    /**
     * json串转list
     *
     * @param json
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonToList(@NonNull String json, Class<T> valueType) {

        try {
            return objectMapper.readValue(json, getCollectionType(valueType));
        } catch (IOException e) {
            log.error("json串转list失败:" + e.toString());
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取泛型的Collection Type
     *
     * @param elementClasses 实体bean
     * @return JavaType Java类型
     */
    private static JavaType getCollectionType(Class<?>... elementClasses) {
        return objectMapper.getTypeFactory().constructParametricType(List.class, elementClasses);
    }

    /**
     * json串转对象
     *
     * @param json
     * @param valueType
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> T jsonToObject(String json, Class<T> valueType) throws Exception {
        try {
            return objectMapper.readValue(json, valueType);
        } catch (IOException e) {
            log.error("json串转对象失败:" + e.toString());
            throw new Exception(e);
        }
    }

    /**
     * Object对象转指定对象
     */
    public static <T> T objectToMap(@NonNull Object obj, Class<T> valueType) {
        return objectMapper.convertValue(obj, valueType);
    }

    /**
     * list转json串
     *
     * @param list
     * @param <T>
     * @return
     */
    public static <T> String listToJson(List<T> list) {
        try {
            return objectMapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            log.error("list转json串失败:" + e.toString());
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象转json串
     *
     * @param object
     * @return
     */
    public static String objectToJson(Object object, Boolean formatFlag) {
        try {
            String jsonStr = objectMapper.writeValueAsString(object);
            if (formatFlag) {
                objectMapper.configure(SerializationFeature.INDENT_OUTPUT, formatFlag);
            }
            return jsonStr;
        } catch (JsonProcessingException e) {
            log.error("对象转json串失败:" + e.toString());
            throw new RuntimeException(e);
        }
    }


}
